<template>
  <t-link theme="primary" @click="handleEngineeringCostDiffView">{{props.engineeringCostDiff.toFixed(2)}}</t-link>
  <t-dialog v-model:visible="visible" attach="body" header="洽商金额" width="70%">
    
    <discuss-log-table
      ref="discussLogTable"
      @open-dialog="handleOpenDialog" 
     :create-params="{ 
        costList:[],
        reduceList:[],
        fileUrl:[],
        collectionId: props.collectionId,
        paymentStage: props.paymentStage,
        orderId: props.orderId
      }"
      :initial-params="{ 
        collectionId: props.collectionId,
        orderId: props.orderId
      }"
      />
      
    <template #footer>
      <t-button theme="default" @click="visible = false">取消</t-button>
      <!-- <t-button :loading="loadingSubmit" @click="handleSelectedIds">保存</t-button> -->
    </template>
  </t-dialog>
  <discuss-log-dialog
    v-if="visible"
    ref="disscussLogDialog"
    :order-id="props.orderId" 
    :is-open-collection="false"
    @request-success="handleDialogRequestSuccess"
  />
</template>

<script setup lang="ts">
import dayjs from 'dayjs';
import { onMounted, ref, useTemplateRef, watch } from 'vue';
import { MessagePlugin, TableCol, TreeNodeModel } from 'tdesign-vue-next';

import { payContractZhengCRUD } from '@/api/finance/pay';
import { discussLogListCRUD,  } from '@/api/project/details';

import { getCompanyList } from '@/api/system/dept';
import DiscussLogTable from './DiscussLogTable.vue';
import DiscussLogDialog from './DiscussLogDialog.vue';



const props = defineProps({
  collectionId: {
    type: String || Number,
    required: true,
  },
  orderId: {
    type: String,
    required: true,
  },
  paymentStage: {
    type: String,
    required: true,
  },
  engineeringCostDiff: {
    type: Number,
    required: true,
  },
});

const emits = defineEmits(['refresh']);

const visible = ref(false);


// 自定义对话框ref
const disscussLogDialog = useTemplateRef('disscussLogDialog');
// 洽商表格
const discussLogTable = useTemplateRef('discussLogTable');


// 处理打开对话框
const handleOpenDialog = (mode: 'create' | 'read' | 'update', params: Record<string, any>) => {
    disscussLogDialog.value.open(mode, params);
  
};
// 处理对话框请求成功
const handleDialogRequestSuccess = (result: Record<string, any>) => {
  if (result.mode === 'create') discussLogTable.value.initTable();
  if (result.mode === 'update') discussLogTable.value.refreshTable();
};
const handleEngineeringCostDiffView = () => {
  visible.value = true;
  discussLogTable.value.initTable();
}

watch(visible, (newVal) => {
  if(newVal===false) {
    emits('refresh');
  }
});
</script>
